package com.qf.flowerlanguage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.flowerlanguage.entity.User;
import com.qf.flowerlanguage.service.UserService;
import com.qf.flowerlanguage.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Objects;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //用户名登录
    @RequestMapping("/login")
    @ResponseBody
    public UserVo loginUser(User user){
        QueryWrapper<User> userQW = new QueryWrapper<>();
        QueryWrapper<User> eq = userQW.eq("uname", user.getUname()).eq("upassword", user.getUpassword());
        User user1 = userService.getOne(eq);
        // 比较密码

        UserVo userVo = new UserVo();
        if (Objects.isNull(user1)){
            userVo.setCode("1");
            userVo.setMessage("登录失败");
        }else {
            userVo.setCode("0");
            userVo.setMessage("登录成功");
            userVo.setData(user1.getUname());
        }
        return userVo;
    }

    //生成短信验证码
    @RequestMapping("/getCode")
    @ResponseBody
    public  String getCode(){
        return "1234";
    }




    //手机号登录
    @RequestMapping("/login2")
    @ResponseBody
    public UserVo loginUser1(User user){
        QueryWrapper<User> userQW = new QueryWrapper<>();
        QueryWrapper<User> uphone = userQW.eq("uphone", user.getUphone());
        User user1 = userService.getOne(uphone);
        // 比较密码

        UserVo userVo = new UserVo();
        if (Objects.isNull(user1)){
            userVo.setCode("1");
            userVo.setMessage("该手机未注册,登录失败");
        }else {
            userVo.setCode("0");
            userVo.setMessage("登录成功");
            userVo.setData(user1.getUname());
        }
        return userVo;
    }
    //手机注册
    @RequestMapping("/phoneRegister")
    @ResponseBody
    public UserVo phoneRegister(User user, HttpSession session){
        QueryWrapper<User> userQW = new QueryWrapper<>();
        QueryWrapper<User> uphone = userQW.eq("uphone", user.getUphone());
        User user1 = userService.getOne(uphone);
        UserVo userVo=new UserVo();
        if (Objects.isNull(user1)){
            //如果查询手机号后    数据库无重复数据    则将其存入数据库中
                userVo.setCode("0");
                userVo.setMessage("注册成功");
                userVo.setData(user.getUphone());
                session.setAttribute("uphone",user.getUphone());
            return userVo;
        }else {
            userVo.setCode("1");
            userVo.setMessage("手机已经存在!!!注册失败");
            return userVo;
        }

    }
    //手机注册二
    @RequestMapping("/phoneRegister1")
    @ResponseBody
    public  UserVo phoneRegister1(User user,HttpSession session){
        //查询用户名  在表中是否存在
        QueryWrapper<User> userQW = new QueryWrapper<>();
        QueryWrapper<User> uname = userQW.eq("uname", user.getUname());
        User one = userService.getOne(uname);
        UserVo userVo=new UserVo();
        //如果  查询结果是null  那么表中无数据   否则表中有数据
        if (Objects.isNull(one)){
            QueryWrapper<User> userQW1 = new QueryWrapper<>();
            QueryWrapper<User> uemail = userQW1.eq("uemail", user.getUemail());

            User one1 = userService.getOne(uemail);
            if (Objects.isNull(one1)){
                String uphone = (String) session.getAttribute("uphone");
                user.setUphone(uphone);
                boolean save = userService.save(user);
                if (save){
                    userVo.setCode("0");
                    userVo.setMessage("注册成功");
                    userVo.setData(user.getUemail());
                }else {
                    userVo.setCode("1");
                    userVo.setMessage("注册失败");
                }
                return userVo;
            }else {
                userVo.setCode("2");
                userVo.setMessage("注册失败");
                return userVo;
            }
        }else {
            userVo.setCode("3");
            userVo.setMessage("注册失败");
            return userVo;
        }
    }




    //账号注册
    @RequestMapping("/idRegister")
    @ResponseBody
    public UserVo idRegister( User user){

        //查询用户名  在表中是否存在
        QueryWrapper<User> userQW = new QueryWrapper<>();
        QueryWrapper<User> uname = userQW.eq("uname", user.getUname());
        User one = userService.getOne(uname);
        UserVo userVo=new UserVo();
        //如果  查询结果是null  那么表中无数据   否则表中有数据
        if (Objects.isNull(one)){
            QueryWrapper<User> userQW1 = new QueryWrapper<>();
            QueryWrapper<User> uemail = userQW1.eq("uemail", user.getUemail());
            User one1 = userService.getOne(uemail);
            if (Objects.isNull(one1)){
                boolean save = userService.save(user);
                if (save){
                    userVo.setCode("0");
                    userVo.setMessage("注册成功");
                    userVo.setData(user.getUemail());
                }else {
                    userVo.setCode("1");
                    userVo.setMessage("注册失败");
                }
                return userVo;
            }else {
                userVo.setCode("2");
                userVo.setMessage("注册失败");
                return userVo;
            }
        }else {
            userVo.setCode("3");
            userVo.setMessage("注册失败");
            return userVo;
        }
    }




}
